import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int n = scanner.nextInt();
        int m = scanner.nextInt();
        int l = scanner.nextInt();
        scanner.nextLine();

        String[] arr = new String[l];
        for (int i = 0; i < l; i++) {
            arr[i] = scanner.nextLine();
            scanner.nextLine();
        }

        Solution solution = new Solution();
        solution.findMaxForm(arr,m,n);
    }



    public static void main1(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int n = scanner.nextInt();
        int[] arr = new int[n];
        for (int i = 0; i < n; i++) {
            arr[i] = scanner.nextInt();
        }

        Solution1 solution = new Solution1();
        System.out.println(solution.minOperations(arr));
    }
}

class Solution {
    public int findMaxForm(String[] str, int m, int n) {
        int x = str.length;
        int[][][] dp = new int[x + 1][m + 1][n + 1];
        int[][] num = new int[x][2];

        for (int i = 0; i < x; i++) {
            char[] arr = str[i].toCharArray();
            for (int j = 0; j < arr.length; j++) {
                if(arr[j] == '0'){
                    num[i][0]++;
                }else {
                    num[i][1]++;
                }
            }
        }

        for (int i = 1; i <= x; i++) {
            for (int j = 0; j <= m; j++) {
                for (int k = 0; k <= n; k++) {
                    dp[i][j][k] = dp[i - 1][j][k];
                    if (j >= num[i - 1][0] && k >= num[i - 1][1]) {
                        dp[i][j][k] = Math.max(dp[i][j][k], dp[i - 1][j - num[i - 1][0]][k - num[i - 1][1]] + 1);
                    }
                }
            }
        }
        return dp[x][m][n];
    }
}

class Solution1 {
    public int gcd(int a,int b){
        while(b != 0){
            int r = a % b;
            a = b;
            b = r;
        }
        return a;
    }

    public int minOperations(int[] nums) {
        int n = nums.length;
        int sum = 0;
        int a = 0;

        for (int k : nums) {
            a = gcd(a,k);
            if (k == 1) {
                sum++;
            }
        }

        if(a > 1){
            return -1;
        }

        if(sum > 0) {
            return n - sum;
        }

        int minSize = n;
        for (int i = 0; i < n; i++) {
            int g = 0;
            for (int j = i; j < n; j++) {
                g = gcd(g, nums[j]);
                if (g == 1) {
                    minSize = Math.min(minSize, j - i);
                    break;
                }
            }
        }

        return n + minSize - 1;
    }
}